package org.itboys.rpc.service.member;


import org.apache.commons.lang3.StringUtils;
import org.itboys.ResultHolder;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.random.RandomUtils;
import org.itboys.constant.ErrorConstant;
import org.itboys.dto.MemberDetail;
import org.itboys.dto.base.Result;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.rpc.constant.RpcConstant;
import org.itboys.rpc.service.BaseRpcService;
import org.itboys.rpc.service.sms.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 会员相关手机接口
 * @author 执行董事长.android资深架构师.王然
 *
 */
@Service
public class RpcMemberService extends BaseRpcService{

	@Autowired
	private MemberService memberService;
	@Autowired
	private ValidateCodeService validateCodeService;
	
	/**
	 * 会员登入
	 * @param mobile 手机号登入
	 * @param password 密码
	 */
	public Result<MemberDetail> login(String mobile, String password) {
		try{
			Member me = memberService.getByField("username", mobile);
			if(me == null){//手机号不存在
				return ResultHolder.getFailResult(ErrorConstant.OBJECT_NOT_EXIST, ErrorConstant.OBJECT_NOT_EXIST_MESSAGE);
			}
			if(!StringUtils.equals(Digests.md5(password), me.getPassword())){//密码错误
				return ResultHolder.getFailResult(ErrorConstant.USER_PSW_NOT_RIGHT_CODE, ErrorConstant.USER_PSW_NOT_RIGHT);
			}
			
			MemberDetail member = new MemberDetail();
			member.setMemberId(me.getId());
			member.setMobile(me.getUsername());
			member.setMobile(me.getUsername());
			member.setLevel(me.getLevel());
			member.setStatus(me.getStatus());
			member.setLogo(me.getLogo());
			
			//会员ID放自定义session
			addToSession(RpcConstant.MEMBER_SESSION_KEY, member.getMemberId());
			return ResultHolder.getSuccessResult(member);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error("memberLogin error mobile = " + mobile + "password = " + password, e);
			return ResultHolder.getExceptionResult();
		}
	}
	
	/**
	 * 会员手机号注册
	 * @param validateCode
	 * @param me
	 * @return 会员信息
	 */
	public Result<MemberDetail> regist(Member me, String validateCode){
		try{
			
			String code = validateCodeService.getCodeReg(me.getUsername());//获取注册验证码
			if(StringUtils.isBlank(code)){//验证码是空的
				return ResultHolder.getFailResult(RpcConstant.VALIDATA_CODE_ERROR, "验证码不能为空");
			}
			if(!StringUtils.equals(code, validateCode)){
				return ResultHolder.getFailResult(RpcConstant.VALIDATA_CODE_ERROR, "验证码错误");
			}
			if(StringUtils.isBlank(me.getPassword())){
				return ResultHolder.getFailResult(RpcConstant.PASSWORD_BLANK, "密码不能为空");
			}
			
			int result = memberService.insert(me, null);
			if(result == -1){
				return ResultHolder.getFailResult(RpcConstant.MOBILE_NO_ISEXIST, "手机号已注册");
			}else if(result == 0){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "注册失败");
			}
			
			MemberDetail member = new MemberDetail();
			member.setMemberId(me.getId());
			member.setMobile(me.getUsername());
			member.setMobile(me.getUsername());
			member.setLevel(me.getLevel());
			member.setStatus(me.getStatus());
			member.setLogo(me.getLogo());

			addToSession(RpcConstant.MEMBER_SESSION_KEY, member.getMemberId());
			return ResultHolder.getSuccessResult(member);
		}catch(Exception e) {
			logger.error("mobileRegist error mobile = " + me.getUsername() + "validateCode = " + validateCode, e);
			return ResultHolder.getExceptionResult();
		}
	}
	
	/**
	 * 修改用户基本信息
	 * @param memberDetail
	 * @return
	 */
	public Result<MemberDetail> updateInfo(MemberDetail memberDetail, MultipartFile logos){
		try{
			
			Member member = new Member();
			member.setId(memberDetail.getMemberId());
			member.setNickname(memberDetail.getNickname());
			member.setLogo(memberDetail.getLogo());
			member.setUsername(memberDetail.getMobile());
			int result = memberService.insert(member, logos);
			if(result == 1){
				return ResultHolder.getSuccessResult(memberDetail);
			}else{
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "修改失败");
			}
		}catch(Exception e) {
			logger.error("updateInfo error memberDetail = " +memberDetail.toString(), e);
			return ResultHolder.getExceptionResult();
		}
	}
	
	/**
	 * 用户找回密码功能
	 * @param mobile
	 * @param validateCode
	 * @return
	 */
	public Result<Void> findPasswd(String mobile,String validateCode){
		
		try{
			String code = validateCodeService.getCodeForgetPwd(mobile);
			if(!StringUtils.equals(code, validateCode)){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "验证码不正确");
			}
			// 重置密码后以短信的形式告诉用户
			int newpwd = RandomUtils.buildRandom(6);//生成6位的随机密码
			int result = memberService.updatePwd(mobile, String.valueOf(newpwd));
			if(result == 0){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "修改密码失败");
			}else if(result == -1){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "手机号不存在");
			}
			logger.info("找回密码成功：mobile={}, validateCode={}, newPassword={}", new Object[]{mobile, validateCode, newpwd});
			return ResultHolder.getSuccessResult(null);
		}catch(Exception e) {
			logger.error("findPwd error mobile = " + mobile + "validateCode = " + validateCode, e);
			return ResultHolder.getExceptionResult();
		}
	}
	
	/**
	 * 用户的密码修改
	 * @param mobile
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 */
	public Result<Void> updatePasswd(String mobile,String oldPwd,String newPwd){
		try{
			int result = memberService.updatePwd(mobile, oldPwd, newPwd);
			if(result == 0){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "修改密码失败");
			}else if(result == -1){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "手机号不存在");
			}else if(result == -2){
				return ResultHolder.getFailResult(ErrorConstant.ACTION_FAIL, "原始密码输入错误");
			}
			return ResultHolder.getSuccessResult(null);
		}catch(Exception e) {
			logger.error("updatePwd error mobile=" + mobile + ",oldPwd=" + oldPwd +",newPwd=" + newPwd, e);
			return ResultHolder.getExceptionResult();
		}
	}

	
}

